package htime

import (
	"regexp"
	"strings"
	"time"
)

func init() {
	Register(NewHDate())
}

var (
	dateSeps = []string{"/", ".", "_", "年", "月", "日"}
)

type hDate struct {
	*regexp.Regexp
}

func NewHDate() StrToTime {
	return &hDate{
		Regexp: regexp.MustCompile(`^[1-9]\d{3}[\.\-\_/年](0{0,1}[1-9]|10|11|12)[\.\-\_/月](0{0,1}[1-9]|1[1-9]|2[1-9]|30|31)[日]{0,1}$`),
	}
}

func (h *hDate) Check(s string) bool {
	return h.FindStringIndex(s) != nil
}

func (h *hDate) Result(t *StrTime) *time.Time {
	for _, s2 := range dateSeps {
		if strings.Index(t.DateTime, s2) != -1 {
			t.DateTime = strings.ReplaceAll(t.DateTime, s2, "-")
		}
	}
	dates := strings.Split(t.DateTime, "-")
	if len(dates[1]) == 1 {
		dates[1] = "0" + dates[1]
	}
	if len(dates[2]) == 1 {
		dates[2] = "0" + dates[2]
	}
	parseTime, _ := time.ParseInLocation("2006-01-02", strings.Join(filterSlices(dates), "-"), getLoc(t))
	return &parseTime
}
